package com.frxuexi.visit.view

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.app.TimePickerDialog
import android.app.TimePickerDialog.OnTimeSetListener
import android.widget.DatePicker
import android.widget.EditText
import android.widget.TimePicker
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.VisitBeanItem
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.net.Const
import com.frxuexi.visit.VisitViewModel
import com.frxuexi.visit.databinding.ActivityVisitAddBinding
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@Route(path = "/visit/VisitAddActivity")
@AndroidEntryPoint
class VisitAddActivity : BaseActivity<ActivityVisitAddBinding,VisitViewModel>() {
    val calendar = Calendar.getInstance()
    override fun initData() {
        vm.addVisitsData.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

    override fun initView() {
        binding.headVisitAdd.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("添加来访")
            headerAdd.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_ok)
            headerAdd.setOnClickListener {
                val date = binding.visitaddDate.text.toString()
                val start = binding.visitaddStart.text.toString()
                val emp = binding.visitaddEmp.text.toString().toInt()
                val card = binding.visitaddCard.text.toString()
                val phone = binding.visitaddPhone.text.toString()
                val carNo = binding.visitaddCarNo.text.toString()
                val purpose = binding.visitaddPurpose.text.toString()
                val item = VisitBeanItem(
                    "${date} ${start}",
                    false,
                    emptyList(),
                    emp,
                    null,
                    card,
                    phone,
                    carNo,
                    purpose,
                    binding.visitaddSwitch.isChecked,
                    "待审核",
                    SPUtils.getInstance().getString(Const.USERNAME)
                    ,1
                )
                vm.addVisit(item)
            }
        }

        binding.visitaddIvdate.setOnClickListener {

            DatePickerDialog(this,object :OnDateSetListener{
                override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                    val m=if(month+1<10) "0${month+1}" else "${month+1}"
                    val day=if(dayOfMonth<10) "0${dayOfMonth}" else "${dayOfMonth}"
                    val date="${year}-${m}-${day}"
                    binding.visitaddDate.setText(date)
                }

            },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show()
        }

        binding.visitaddIvstart.setOnClickListener {
            showTime(binding.visitaddStart)
        }
        binding.visitaddIvend.setOnClickListener {
            showTime(binding.visitaddEnd)
        }

        binding.visitaddSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.visitaddCarNo.isEnabled=isChecked
        }
    }

    fun showTime(et:EditText){
        TimePickerDialog(this,object :OnTimeSetListener{
            override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {
                val h=if(hourOfDay<10) "0${hourOfDay}" else "${hourOfDay}"
                val m=if(minute<10) "0${minute}" else "${minute}"
                et.setText("${h}:${m}:00")
            }
        },calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show()
    }



}